<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Dialogs</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Visual Basic, dialogs, GTK#, programming, Linux">
<meta name="description" content="Dialogs">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div> 


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Dialogs</h1>


<p>
In this part of the Visual Basic GTK# programming tutorial, we will introduce dialogs. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications.
A dialog is defined as a conversation between two or more persons. In a computer
application a dialog is a window which is used to "talk" to the application.
A dialog is used to input data, modify data, change the application settings etc.
Dialogs are important means of communication between a user and a computer program.
</p>


<h2>Message boxes</h2>

<p>
Message dialogs are convenient dialogs that provide messages to the user of the application.
The message consists of textual and image data. 
</p>

<pre class="code">
' ZetCode Mono Visual Basic GTK# tutorial
'
' This program shows message dialogs.
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Gtk
 
Public Class GtkVBApp
    Inherits Window
 
    Public Sub New
        
        MyBase.New("Message dialogs")
        
        Me.InitUI
        
        Me.SetDefaultSize(250, 100)
        Me.SetPosition(WindowPosition.Center)
        AddHandler Me.DeleteEvent, AddressOf Me.OnDelete

        Me.ShowAll  
        
    End Sub
    
    Private Sub InitUI
    
        Dim table As New Table(2, 2, True)
        
        Dim info As New Button("Information")
        Dim warn As New Button("Warning")
        Dim ques As New Button("Question")
        Dim erro As New Button("Error")

        AddHandler info.Clicked, AddressOf Me.OnInfo
        AddHandler warn.Clicked, AddressOf Me.OnWarning
        AddHandler ques.Clicked, AddressOf Me.OnQuestion
        AddHandler erro.Clicked, AddressOf Me.OnError
    
        table.Attach(info, 0, 1, 0, 1)
        table.Attach(warn, 1, 2, 0, 1)
        table.Attach(ques, 0, 1, 1, 2)
        table.Attach(erro, 1, 2, 1, 2)
        
        Me.Add(table)

    End Sub
    
    Private Sub OnInfo(ByVal sender As Object, ByVal args As EventArgs)
        Dim md As MessageDialog = New MessageDialog(Me, _
            DialogFlags.DestroyWithParent, MessageType.Info, _ 
            ButtonsType.Close, "Download completed")
        md.Run
        md.Destroy
    End Sub
    
    Private Sub OnWarning(ByVal sender As Object, ByVal args As EventArgs)
        Dim md As MessageDialog = New MessageDialog(Me, _
            DialogFlags.DestroyWithParent, MessageType.Warning, _
            ButtonsType.Close, "Unallowed operation")
        md.Run
        md.Destroy
    End Sub
    
    Private Sub OnQuestion(ByVal sender As Object, ByVal args As EventArgs)
        Dim md As MessageDialog = New MessageDialog(Me, _
            DialogFlags.DestroyWithParent, MessageType.Question, _
            ButtonsType.Close, "Are you sure to quit?")
        md.Run
        md.Destroy
    End Sub
    
    Private Sub OnError(ByVal sender As Object, ByVal args As EventArgs)
        Dim md As MessageDialog = New MessageDialog(Me, _
            DialogFlags.DestroyWithParent, MessageType.Error, _
            ButtonsType.Close, "Error loading file")
        md.Run
        md.Destroy
    End Sub
   
    Private Sub OnDelete(ByVal sender As Object, _
            ByVal args As DeleteEventArgs)
        Application.Quit
    End Sub

    Public Shared Sub Main
    
        Application.Init
        Dim app As New GtkVBApp
        Application.Run
        
    End Sub
 
End Class
</pre>

<p>
In our example, we will show four kinds of message dialogs. Information, Warning, Question and Error message
dialogs.
</p>

<pre class="explanation">
 Dim info As New Button("Information")
 Dim warn As New Button("Warning")
 Dim ques As New Button("Question")
 Dim erro As New Button("Error")
</pre>

<p>
We have four buttons. Each of these buttons will show a different kind of message dialog. 
</p>

<pre class="explanation">
 Private Sub OnInfo(ByVal sender As Object, ByVal args As EventArgs)
     Dim md As MessageDialog = New MessageDialog(Me, _
         DialogFlags.DestroyWithParent, MessageType.Info, _ 
         ButtonsType.Close, "Download completed")
     md.Run
     md.Destroy
 End Sub
</pre>

<p>
If we click on the info button, the Information dialog is displayed. 
The <b class="keyword">MessageType.Info</b> specifies the type of the
dialog. The <b class="keyword">ButtonsType.Close</b> specifies the button to 
be displayed in the dialog. The last parameter is the message dislayed. 
The dialog is displayed with the <b class="keyword">Run</b> method. 
The programmer must also call either the <b class="keyword">Destroy</b> or the 
<b class="keyword">Hide</b> method.
</p>


<br>
<img src="/img/gui/vbgtk/information.png" alt="Information message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbgtk/warning.png" alt="Warning message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbgtk/question.png" alt="Question message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbgtk/error.png" alt="Error message dialog">
<br>
<br>


<h2>AboutDialog</h2>

<p>
The <b class="keyword">AboutDialog</b> displays information about the application. <b class="keyword">AboutDialog</b>
can display a logo, the name of the application, version, copyright, website or licence information. 
It is also possible to give credits to the authors, documenters, translators and artists.
</p>

<pre class="code">
' ZetCode Mono Visual Basic GTK# tutorial
'
' This program shows the about
' dialog
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Gtk
 
Public Class GtkVBApp
    Inherits Window
 
    Public Sub New
        
        MyBase.New("About dialog")
        
        Me.InitUI
        
        Me.SetDefaultSize(350, 300)
        Me.SetPosition(WindowPosition.Center)
        AddHandler Me.DeleteEvent, AddressOf Me.OnDelete

        Me.ShowAll
        
    End Sub
    
    Private Sub InitUI
    
        Dim button As New Button("About")
        
        AddHandler button.Clicked, AddressOf Me.ShowDialog
        
        Dim fixed As New Fixed
        fixed.Put(button, 20, 20)
        Me.Add(fixed)

    End Sub
    
    Private Sub ShowDialog(ByVal sender As Object, _
            ByVal args As EventArgs)
    
        Dim about As New AboutDialog
        about.ProgramName = "Battery"
        about.Version = "0.1"
        about.Copyright = "(c) Jan Bodnar"
        about.Comments = "Battery is a simple tool for battery checking"
        about.Website = "http://www.zetcode.com"
        about.Logo = New Gdk.Pixbuf("battery.png")
        about.Run
        about.Destroy

    End Sub

   
    Sub OnDelete(ByVal sender As Object, _
            ByVal args As DeleteEventArgs)
        Application.Quit
    End Sub

    Public Shared Sub Main
    
        Application.Init
        Dim app As New GtkVBApp
        Application.Run
        
    End Sub
 
End Class
</pre>

<p>
The code example uses a <b class="keyword">AboutDialog</b> with some of its features. 
</p>

<pre class="explanation">
 Dim about As New AboutDialog
</pre>

<p>
We create an <b class="keyword">AboutDialog</b>. 
</p>

<pre class="explanation">
 Dim about As New AboutDialog
 about.ProgramName = "Battery"
 about.Version = "0.1"
 about.Copyright = "(c) Jan Bodnar"
</pre>

<p>
By setting the properties of the dialog, we specify the name, version and the copyright.
</p>

<pre class="explanation">
 about.Logo = New Gdk.Pixbuf("battery.png")
</pre>

<p>
This line creates a logo. 
</p>


<br>
<img src="/img/gui/vbgtk/about.png" alt="AboutDialog">
<div class="figure">Figure: AboutDialog</div>
<br>


<h2>FontSelectionDialog</h2>

<p>
The <b class="keyword">FontSelectionDialog</b> is a dialog for selecting fonts. 
It is typically used in applications, that do some text editing or formatting. 
</p>

<pre class="code">
' ZetCode Mono Visual Basic GTK# tutorial
'
' This program shows the FontSelectionDialog
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com

Imports Gtk


Public Class GtkVBApp
    Inherits Window
 
    Dim label As Label 
 
    Public Sub New
        
        MyBase.New("Font dialog")
        
        Me.InitUI
        
        Me.SetDefaultSize(350, 300)
        Me.SetPosition(WindowPosition.Center)
        AddHandler Me.DeleteEvent, AddressOf Me.OnDelete

        Me.ShowAll  
        
    End Sub
    
    Private Sub InitUI 
    
        label = New Label("The only victory over love is flight.")
        Dim button As New Button("Select font")
        
        AddHandler button.Clicked, AddressOf Me.ShowDialog

        Dim fixed As New Fixed
        fixed.Put(button, 100, 30)
        fixed.Put(label, 30, 90)
        Me.Add(fixed)

    End Sub
    
    Private Sub ShowDialog(ByVal sender As Object, _
            ByVal args As EventArgs)
    
        Dim fdia As New FontSelectionDialog("Select font name")
        
        AddHandler fdia.Response, AddressOf Me.SelectFont
     
        fdia.Run
        fdia.Destroy

    End Sub
    
    Private Sub SelectFont(ByVal sender As Object, _
            ByVal args As ResponseArgs)
        
        If args.ResponseId = ResponseType.Ok
            Dim fontdesc As Pango.FontDescription = _
                Pango.FontDescription.FromString(sender.FontName)
            label.ModifyFont(fontdesc)
        End If
    
    End Sub
   
    Sub OnDelete(ByVal sender As Object, _
            ByVal args As DeleteEventArgs)
        Application.Quit
    End Sub

    Public Shared Sub Main
    
        Application.Init
        Dim app As New GtkVBApp
        Application.Run
        
    End Sub
 
End Class
</pre>

<p>
In the code example, we have a button and a label. We show the 
<b class="keyword">FontSelectionDialog</b> by clicking on the button.
</p>

<pre class="explanation">
 Dim fdia As New FontSelectionDialog("Select font name")
</pre>

<p>
We create the <b class="keyword">FontSelectionDialog</b>.
</p>

<pre class="explanation">
 If args.ResponseId = ResponseType.Ok
     Dim fontdesc As Pango.FontDescription = _
         Pango.FontDescription.FromString(sender.FontName)
     label.ModifyFont(fontdesc)
 End If
</pre>

<p>
If we click on the OK button, the font of the label widget changes to the
one, that we selected in the dialog. 
</p>

<br>
<img src="/img/gui/vbgtk/fontdialog.png" alt="FontSelectionDialog">
<div class="figure">Figure: FontSelectionDialog </div>
<br>


<h2>ColorSelectionDialog</h2>

<p>
<b class="keyword">ColorSelectionDialog</b> is a dialog for selecting a color. 
</p>

<pre class="code">
' ZetCode Mono Visual Basic GTK# tutorial
'
' This program shows the ColorSelectionDialog
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Gtk
 
Public Class GtkVBApp
    Inherits Window
 
    Dim label As Label 
 
    Public Sub New
        
        MyBase.New("Color dialog")
        
        Me.InitUI
        
        Me.SetDefaultSize(350, 300)
        Me.SetPosition(WindowPosition.Center)
        AddHandler Me.DeleteEvent, AddressOf Me.OnDelete

        Me.ShowAll    
        
    End Sub
    
    Private Sub InitUI
    
        label = New Label("The only victory over love is flight.")
        Dim button As New Button("Select color")
        
        AddHandler button.Clicked, AddressOf Me.ShowDialog

        Dim fixed As New Fixed
        fixed.Put(button, 100, 30)
        fixed.Put(label, 30, 90)
        Me.Add(fixed)

    End Sub
    
    Private Sub ShowDialog(ByVal sender As Object, _
            ByVal args As EventArgs)
    
        Dim cdia As New ColorSelectionDialog("Select color")
        
        AddHandler cdia.Response, AddressOf Me.SelectColor
     
        cdia.Run
        cdia.Destroy

    End Sub
    
    Private Sub SelectColor(ByVal sender As Object, _
            ByVal args As ResponseArgs)

        If args.ResponseId = ResponseType.Ok
            label.ModifyFg(StateType.Normal, _
                sender.ColorSelection.CurrentColor)
        End If
    
    End Sub

   
    Sub OnDelete(ByVal sender As Object, _
            ByVal args As DeleteEventArgs)
        Application.Quit
    End Sub

    Public Shared Sub Main
    
        Application.Init
        Dim app As New GtkVBApp
        Application.Run
        
    End Sub
 
End Class
</pre>

<p>
The example is very similar to the previous one. This time we change the color of the label. 
</p>

<pre class="explanation">
 Dim cdia As New ColorSelectionDialog("Select color")
</pre>

<p>
We create the <b class="keyword">ColorSelectionDialog</b>.
</p>

<pre class="explanation">
 If args.ResponseId = ResponseType.Ok
     label.ModifyFg(StateType.Normal, _
         sender.ColorSelection.CurrentColor)
 End If
</pre>

<p>
If the user pressed OK, we get the color value and modify the label's color. 
</p>

<br>
<img src="/img/gui/vbgtk/colordialog.png" alt="ColorSelectionDialog">
<div class="figure">Figure: ColorSelectionDialog </div>
<br>

<hr class="btm">

<p>
In this part of the Visual Basic GTK# tutorial, we presented dialogs. 
</p>

<br> 
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified May 25, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>


